package com.jimmy.study.jmx;

import java.util.ArrayList;
import java.util.List;

import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedOperationParameter;
import org.springframework.jmx.export.annotation.ManagedOperationParameters;
import org.springframework.jmx.export.annotation.ManagedResource;

//实例标记为由JMX管理的资源
@ManagedResource(objectName = "bean:name=testJmxBean", description = "MyManagedBean", log = true, logFile = "jmx.log", currencyTimeLimit = 15, persistPolicy = "OnUpdate", persistPeriod = 200, persistLocation = "foo", persistName = "bar")
public class AnnotationTestBean {

	private String name;
	private int age;

	private List<String> values;

	// 把getter或setter标记为JMX的属性
	@ManagedAttribute(description = "The Age Attribute", currencyTimeLimit = 1)
	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@ManagedAttribute(description = "The values Attribute", currencyTimeLimit = 1)
	public List<String> getValues() {
		values = new ArrayList<String>(2);
		values.add("hello");
		values.add("world");
		return values;
	}

	@ManagedAttribute(description = "The Name Attribute", currencyTimeLimit = 20, defaultValue = "bar", persistPolicy = "OnUpdate")
	public void setName(String name) {
		this.name = name;
		System.out.println("set:" + name);
	}

	@ManagedAttribute(defaultValue = "foo", persistPeriod = 300)
	public String getName() {
		System.out.println("get:" + name);
		return name;
	}

	// 把方法标记为JMX的操作
	@ManagedOperation(description = "Add two numbers")
	@ManagedOperationParameters({
			@ManagedOperationParameter(name = "x", description = "The first number"),
			@ManagedOperationParameter(name = "y", description = "The second number") })
	public int add(int x, int y) {
		return x + y;
	}

	public void dontExposeMe() {
		throw new RuntimeException();
	}
}
